﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Input;

namespace PonGun
{
    public class GameScreen : Screen
    {
        SoundEffectInstance music;

        bool GameOver { get; set; }

        TimeSpan GODuration = new TimeSpan(0, 0, 0, 3, 0);
        TimeSpan GOStart = TimeSpan.Zero;
        bool GOOver { get { return (GODuration - (DateTime.Now.TimeOfDay - GOStart)) < TimeSpan.Zero; } }


        public GameScreen(Game1 game, GSM gsm)
            : base(game, gsm)
        {
            FadeSpeed = .1f;
            HasMouse = false;
            StarFieldOn = false;
        }

        public override void Update(GameTime gameTime)
        {
            StarFieldOn = Game1.ScreenRotating;

            if (State == ScreenStates.FadingIn)
            {
                if (music == null)
                {
                    music = Game.Content.Load<SoundEffect>("Audio/Music/PonGunInGameMusic2").CreateInstance();
                    music.Volume = GSM.musicMaxVolume/2f;
                    music.IsLooped = true;
                }

                if(music.State == SoundState.Stopped)
                    music.Play();

                if(GSM.music.Volume > 0)
                    GSM.music.Volume -= FadeSpeed;
                if (GSM.music.Volume - FadeSpeed < 0)
                    GSM.music.Volume = 0;
            }
            if (State == ScreenStates.FadingOut)
            {
                if (music.State != SoundState.Stopped)
                    music.Stop();

                if (GSM.music.Volume > GSM.musicMaxVolume)
                    GSM.music.Volume = GSM.musicMaxVolume;

                if (GSM.music.Volume < GSM.musicMaxVolume)
                    GSM.music.Volume += FadeSpeed;
            }

            if ((Game.IsKeyPressed(Keys.Escape) || Game.thisP1GP.Buttons.Back == ButtonState.Pressed || Game.thisP2GP.Buttons.Back == ButtonState.Pressed) ||
                (GameOver && (Game.thisKB.GetPressedKeys().Length > 0)))
            {
                if(GOOver)
                    GSM.NavigateTo("MenuScreen");
            }

            if (GameOver)
                return;

            if (Game.lstGTSeconds != (int)gameTime.TotalGameTime.TotalSeconds)
            {
                Game.Bats[0].Score += 1 * Game.Level;
                Game.Bats[1].Score += 1 * Game.Level;

                Game.lstGTSeconds = (int)gameTime.TotalGameTime.TotalSeconds;
            }

            foreach (Bat bat in Game.Bats)
                bat.Update(gameTime);

            foreach (Ball ball in Game.Balls)
            {
                ball.Speed = Game.Level + 1;
                ball.Update(gameTime);
            }

            foreach (Block testBlock in Game.Blocks)
                testBlock.Update(gameTime);

            foreach (City city in Game.Cities)
                city.Update(gameTime);

            collisionManager.Update(gameTime);

            Game.testEmitter.Update(gameTime);

            if (!GameOver && (Game.Cities.Where(city => city.Player == PlayerIndex.One && city.Destroyed).ToList().Count == 4 ||
                Game.Cities.Where(city => city.Player == PlayerIndex.Two && city.Destroyed).ToList().Count == 4))
            {
                Game.Content.Load<SoundEffect>("Audio/SFX/gameOver").Play();
                Game1.ScreenEffectStart = DateTime.Now.TimeOfDay - Game1.ScreenEffectDuration;
                GOStart = DateTime.Now.TimeOfDay;
                GameOver = true;
            }

        }

        public override void CloseDown()
        {
            GameOver = false;
            Game.ReSetGame();
        }

        public override void Draw(GameTime gameTime)
        {
            // The Field
            Game.field.Draw(gameTime);

            Game.RenderScene(gameTime);

            if (GameOver)
            {
                Vector2 wbl = new Vector2(MathHelper.Lerp(-4, 4, (float)Game1.rnd.NextDouble()), MathHelper.Lerp(-4, 4, (float)Game1.rnd.NextDouble()));
                Vector2 pos = Game.Content.Load<SpriteFont>("fonts/GameOverFont").MeasureString("! GAME OVER !") / 2;

                string winText = string.Empty;
                // Who won?
                if (Game.Cities.Where(city => city.Player == PlayerIndex.One && city.Destroyed).ToList().Count == 4)
                {
                    winText = "Player 2 Wins!";

                    Game.spriteBatch.DrawString(Game.Content.Load<SpriteFont>("fonts/GameOverFont"), "! GAME OVER !", Game1.RenderAreaCenter - pos + wbl + new Vector2(-2, 2), Game1.Player1Color);
                    Game.spriteBatch.DrawString(Game.Content.Load<SpriteFont>("fonts/GameOverFont"), "! GAME OVER !", Game1.RenderAreaCenter - pos + wbl, Game1.Player2Color);

                    Vector2 pos2 = Game.Content.Load<SpriteFont>("fonts/GameOverFont").MeasureString(winText) / 2;
                    Game.spriteBatch.DrawString(Game.Content.Load<SpriteFont>("fonts/GameOverFont"), winText, Game1.RenderAreaCenter - pos2 - wbl + new Vector2(-2, 2) + new Vector2(0, 128), Game1.Player1Color);
                    Game.spriteBatch.DrawString(Game.Content.Load<SpriteFont>("fonts/GameOverFont"), winText, Game1.RenderAreaCenter - pos2 - wbl + new Vector2(0, 128), Game1.Player2Color);
                }

                if (Game.Cities.Where(city => city.Player == PlayerIndex.Two && city.Destroyed).ToList().Count == 4)
                {
                    winText = "Player 1 Wins!";

                    Game.spriteBatch.DrawString(Game.Content.Load<SpriteFont>("fonts/GameOverFont"), "! GAME OVER !", Game1.RenderAreaCenter - pos + wbl + new Vector2(-2, 2), Game1.Player2Color);
                    Game.spriteBatch.DrawString(Game.Content.Load<SpriteFont>("fonts/GameOverFont"), "! GAME OVER !", Game1.RenderAreaCenter - pos + wbl, Game1.Player1Color);

                    Vector2 pos2 = Game.Content.Load<SpriteFont>("fonts/GameOverFont").MeasureString(winText) / 2;
                    Game.spriteBatch.DrawString(Game.Content.Load<SpriteFont>("fonts/GameOverFont"), winText, Game1.RenderAreaCenter - pos2 - wbl + new Vector2(-2, 2) + new Vector2(0, 128), Game1.Player2Color);
                    Game.spriteBatch.DrawString(Game.Content.Load<SpriteFont>("fonts/GameOverFont"), winText, Game1.RenderAreaCenter - pos2 - wbl + new Vector2(0, 128), Game1.Player1Color);
                }

                if (Game.Cities.Where(city => city.Player == PlayerIndex.One && city.Destroyed).ToList().Count == 4 && Game.Cities.Where(city => city.Player == PlayerIndex.Two && city.Destroyed).ToList().Count == 4)
                {
                    winText = "! DRAW !";
                    Game.spriteBatch.DrawString(Game.Content.Load<SpriteFont>("fonts/GameOverFont"), "! GAME OVER !", Game1.RenderAreaCenter - pos + wbl + new Vector2(-2, 2), Color.Black);
                    Game.spriteBatch.DrawString(Game.Content.Load<SpriteFont>("fonts/GameOverFont"), "! GAME OVER !", Game1.RenderAreaCenter - pos + wbl, Color.Gold);

                    Vector2 pos2 = Game.Content.Load<SpriteFont>("fonts/GameOverFont").MeasureString(winText) / 2;
                    Game.spriteBatch.DrawString(Game.Content.Load<SpriteFont>("fonts/GameOverFont"), winText, Game1.RenderAreaCenter - pos2 - wbl + new Vector2(-2, 2) + new Vector2(0, 128), Color.Black);
                    Game.spriteBatch.DrawString(Game.Content.Load<SpriteFont>("fonts/GameOverFont"), winText, Game1.RenderAreaCenter - pos2 - wbl + new Vector2(0, 128), Color.Gold);
                }
                
            }
        }        
    }
}
